//
//  CompanyMessageVC.m
//  New_Runway
//
//  Created by mack on 2018/2/1.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CompanyMessageVC.h"
#import "CompanyMessageCell.h"

@interface CompanyMessageVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSString *userName;

@end

@implementation CompanyMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"公司信息";
    [self.navigationBarView.rightItem setTitle:@"保存" forState:UIControlStateNormal];
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
}

-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = UIColorHex(0xf5f5f5);
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.tableview registerClass:[CarSetListCell class] forCellReuseIdentifier:@"CarSetListCell"];
    [self.tableview registerNib:[UINib nibWithNibName:@"CompanyMessageCell" bundle:nil] forCellReuseIdentifier:@"CompanyMessageCell"];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_equalTo(self.navH );
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
 
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    NSArray *array = @[@"联系人",@"手机号",@"微信二维码",@"公司名称"];
    CompanyMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CompanyMessageCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleL.text = array[indexPath.row];
    cell.rightTF.delegate = self;
    cell.rightTF.userInteractionEnabled = NO;
    cell.rightTF.tag = indexPath.row;
    if (indexPath.row == 0) {
        cell.rightTF.userInteractionEnabled = YES;
        cell.rightTF.text = [NRWCarUser sharedUser].userName;
    }else if (indexPath.row == 1){
        cell.rightTF.text = [NRWCarUser sharedUser].userMobile;
    }else if (indexPath.row == 2){
        
    }else if (indexPath.row == 3){
        cell.rightTF.text = [NRWCarUser sharedUser].marketName;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        
    }
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField.tag == 0) {
        _userName = textField.text;
    }
}
-(void)rightItemClick{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
   
    [self updataUser];
    
}
-(void)updataUser{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,updataUserApi];
    NSDictionary *params = @{
                             @"userId":[NRWCarUser sharedUser].userId,
                             @"userName":_userName
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            NRWCarUser *user = [NRWCarUser sharedUser];
            [user setValue:_userName forKey:@"userName"];
            [user archive];
            [self.navigationController popViewControllerAnimated:YES];
            self.success();
        }else{
      
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];

        }
        
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
